<template>
    <el-container class="home-container">
      <el-header>
        <div>
          <img src="../assets/一杯仙气1.png" class="logoimg">
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>

      <el-container>
<!--        侧边栏菜单区-->
        <el-aside  :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="togleCollapse">|||</div>
          <el-menu
            background-color="#363d40"
            text-color="#fff"
            active-text-color="#409FFF"
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="activePath">
<!--            一级菜单-->
<!--            index也需要动态绑定，但：index只接收字符串，最简单的方式就是后面添加一个字符串(字符和字符串拼接会变成字符串-->
            <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
<!--              一级菜单的模板区域-->
              <template slot="title">
<!--                图标-->
                <i :class="iconObj[item.id]"></i>
                <!--                一级菜单名文本-->
                <span>{{ item.authName }}</span>
              </template>

<!--                二级菜单-->
                <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id">
                  <!-- 导航开启路由模式：
将index值作为导航路由 -->
                  <!-- 二级菜单的模板区域 -->
                  <template slot="title">
                    <i class="el-icon-menu"></i>
                    <!--  二级菜单名文本-->
                    <span>{{ subItem.authName}}</span>
                  </template>
                </el-menu-item>

              <!--              三级菜单-->
<!--              <el-submenu index="1-4">-->
<!--                <template slot="title">选项4</template>-->
<!--                <el-menu-item index="1-4-1">选项1</el-menu-item>-->
<!--              </el-submenu>-->
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  data() { //  将获取到的数据封装进来
    return {
      menuList: [ //  左侧菜单数据
      ],
      iconObj: {
        125: 'iconfont ele-icon-oonhaoyou',
        103: 'iconfont ele-icon-oondunpai',
        101: 'iconfont ele-icon-oonpengyouquan',
        102: 'iconfont ele-icon-oonbianjiziliao',
        145: 'iconfont ele-icon-oonxiazai-01'
      },
      // 默认不折叠
      isCollapse: false,
      // 被激活导航地址
      activePath: ''
    }
  },

  created() {
    this.getMenuList() //  调用getMenuList来获取所有的左侧菜单
  },
  methods: {
    logout () {
      window.sessionStorage.clear()//  清空会话 删除token
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus') //  get请求获取 。get函数返回值是promise 所以用await和async来简化它的操作
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      // console.log(res)
    },
    // 菜单的折叠与展开
    togleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存连接的激活地址
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>
<style lang="less" scoped>
  .home-container{
    height: 100%;
  }
  .el-header {
    background-color: #363d40;
    display: flex;
    justify-content: space-between;/*左右对齐*/
    padding:0 15px;/*logo左顶*/
    align-items: center;/*标题居中*/
    color: #fff;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;
      img {
        height: 40px;
      }
      span {
        margin-left: 25px;
      }
    }
  }
  .el-aside {
    background-color: #363d40;
  }

  .el-main {
    background-color: #E9EEF3;
  }
  .el-menu {
    border: none;
  }
  .el-main {
    background-color: #eaedf1;
  }
  .iconfont{
    margin-right: 10px;
  }
  .toggle-button {
    background-color: #4A5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    // 鼠标放上去变成小手
    cursor: pointer;
  }

</style>
